import { ChangeDetectionStrategy, ChangeDetectorRef, Component, OnInit } from '@angular/core';
import { TreeNode } from '@iux/live';
const treeData = [
  {
    label: 'Documents-disabled',
    disabled: true,
    showCheckbox: false,
    children: [
      {
        label: 'Expenses.doc',
        isLeaf: true,
      },
      {
        label: 'Resume.doc',
        isLeaf: true,
      },
    ],
  },
  {
    label: 'Word',
    showCheckbox: false,
    children: [
      {
        label: 'Expenses.doc',
        isLeaf: true,
      },
      {
        label: 'Resume.doc',
        isLeaf: true,
      },
    ],
  },
  {
    label: 'Pictures',
    showCheckbox: false,
    children: [
      {
        label: 'logo.png',
        isLeaf: true,
      },
      {
        label: 'banner.pnd',
        isLeaf: true,
      },
    ],
  },
];

@Component({
  selector: 'app-async',
  templateUrl: './async.component.html',
  styles: [
    `
      .treeselect-container {
        width: 3.68rem;
      }
    `,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AsyncComponent implements OnInit {
  loading: boolean = false;
  treeData: TreeNode[];

  constructor(private cdr: ChangeDetectorRef) {}

  ngOnInit(): void {}

  queryNodeList = (): boolean | Promise<void> => {
    if (!this.treeData?.length) {
      this.loading = true;
      this.cdr.markForCheck();
      return new Promise(resolve => {
        setTimeout(() => {
          resolve(treeData);
        }, 1000);
      }).then((res: TreeNode[]) => {
        this.treeData = res;
        this.loading = false;
        this.cdr.markForCheck();
      });
    } else {
      return true;
    }
  };
}

